package com.wlz.consumer.consumer;

import com.wlz.api.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;

/** dubbo 负载均衡
 *
 * 1. RoundRobin LoadBalance        轮询      (roundrobin)
 * 2. LeastActive LoadBalance       最少活跃调用数 (leastactive)
 * 3. ConsistentHash LoadBalance    一致性 Hash  (consistenthash)
 * 4. Random LoadBalance            随机         (random)
 *
 * @author wlz
 * @date 2022-07-12  9:48 下午
 */
@Slf4j
@EnableAutoConfiguration
public class LoadBalanceDubboConsumerDemo {

    @Reference(version = "default", loadbalance = "consistenthash")
    private DemoService demoService;

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(LoadBalanceDubboConsumerDemo.class, args);

        DemoService demoService = context.getBean(DemoService.class);

        // 用来负载均衡 
     /*   for (int i = 0; i < 1000; i++) {
            try {
                String result = demoService.sayHello("wlz zzz " + i);
                log.info("result:  {}", result);
                Thread.sleep(1* 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }*/
        
        // 一致性hash 算法测试 
        for (int i = 0; i < 1000; i++) {
            String result = demoService.sayHello(i%5 + "wlz zzz ");
            log.info("result:  {}", result);
            try {
                Thread.sleep(1* 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
